module.exports = function ($scope, $resource, toaster, $modalInstance, class_code) {
  
  $scope.pageInfo = {}
  $scope.bigCurrentPage = 1;
  $scope.itemsPerPage = 10;
  $scope.typelist = [];
  $scope.markedMap = {};

  $scope.load = function () {
      var para = {
          'product_name': $scope.pageInfo.product_name,
          'place_name': $scope.pageInfo.place_name,
          'product_code': $scope.pageInfo.product_code,
          'pageNo': $scope.bigCurrentPage, 
          'pageSize': $scope.itemsPerPage,
          'class_code': class_code
      };
      $resource('/api/as/mc/shopclass/noClassProductPageList', {}, {}).save(para, function (res) {
          if (res.errcode === 0) {
              $scope.noClassProductList = res.data.results;
              $scope.bigTotalItems = res.data.totalRecord;
              if($scope.markedMap){
                $scope.noClassProductList.forEach(element => {
                    if($scope.markedMap['a'+element.product_code] == 1){
                        element.mark = true;
                    }
                });
              }
          } else {
              toaster.error({ title: '', body: res.errmsg });
          }
      });
  }
  $scope.load();
  
  $scope.change = function(obj){
      if(obj.mark){
          obj.class_code = class_code;
          obj.asrot = 0;
          $scope.typelist.push(obj);
          $scope.markedMap['a'+obj.product_code] = 1;
      } else {
          for(var i = 0; i < $scope.typelist.length; i++){
              if(obj.product_code == $scope.typelist[i].product_code){
                  $scope.markedMap['a'+$scope.typelist[i].product_code] = 0;
                  $scope.typelist.splice(i,1);
                  break;
              }
          }
      }
  }

  //添加分类下产品
  $scope.save = function(){
      if($scope.typelist.length == 0){
        toaster.warning({title: '', body: '请选择要添加的产品'});
        return;
      }
      var para = {
        'insertlist': $scope.typelist
      }
      $resource('/api/as/mc/shopclass/saveClassProduct', {}, {}).save(para, function (res) {
          if (res.errcode === 0) {
              toaster.success({ title: '', body: '操作成功' });
              $modalInstance.close();
          } else {
              toaster.error({ title: '', body: res.errmsg });
          }
      });
  }

  //取消
  $scope.close = function () {
      $modalInstance.dismiss('cancel');
  }

};